﻿import com.troyworks.datastructures.btree.*;
//Testing the Tree
//Listing 4. This simple program demonstrates how to add items to the bTree, as well as how to delete items, search, and traverse the tree. 
class test.com.troyworks.datastructures.btree.testbTree {
	public static function main(argz:Array):Void {
		var i:Number;
		var myKey:bTreeStringKey;
		var myData:String;
		var myBtree:bTree;
		// Create a bTree object
		myBtree = new bTree("Test");
		var test = 2;
		switch (test) {
		case 1 :
			// Add three keys
			myKey = new bTreeStringKey();
			myKey.key = "Rick";
			myData = new String("Judy");
			myBtree.addItem(myKey, myData);
			myKey = new bTreeStringKey();
			myKey.key = "Gene";
			myData = new String("Sandi");
			myBtree.addItem(myKey, myData);
			myKey = new bTreeStringKey();
			myKey.key = "Phil";
			myData = new String("Kate");
			myBtree.addItem(myKey, myData);
			// Following returns "Sandi"
			trace("following returns Sandi");
			myKey = new bTreeStringKey();
			myKey.key = "Gene";
			myData = String(myBtree.seek(myKey));
			trace(myData);
			// Step through the tree
			trace("step through the tree---------------------");
			myBtree.rewind();
			while (myBtree.gotoNextKey() == 0) {
				trace(myBtree.currentObject);
			}
			// Delete "Phil"
			myKey = new bTreeStringKey();
			myKey.key = "Phil";
			myBtree.removeItem(myKey);
			// Step through the tree backward
			// "Kate" will be gone
			trace("step through backwards 'kate' will be gone--------");
			myBtree.toEnd();
			while (myBtree.gotoPrevKey() == 0) {
				trace(myBtree.currentObject);
				break;
			}
			break;
		case 2 :
			// Add theses out of order
			myBtree.addItem(new bTreeStringKey("AA"), "AAData");
			myBtree.addItem(new bTreeStringKey("DC"), "DCData");
			myBtree.addItem(new bTreeStringKey("AB"), "ABData");
			myBtree.addItem(new bTreeStringKey("AD"), "ADData");
			myBtree.addItem(new bTreeStringKey("AC"), "ACData");
			myBtree.addItem(new bTreeStringKey("AE"), "AEData");
			// Step through the tree
			trace("Goto Beginning step through the tree---------------------");
			myBtree.rewind();
			while (myBtree.gotoNextKey() == 0) {
				trace(myBtree.currentKey+"  = "+myBtree.currentObject);
			}
			trace("Goto End step through the tree---------------------");
			myBtree.toEnd();
			while (myBtree.gotoPrevKey() == 0) {
				trace(myBtree.currentKey+"  = "+myBtree.currentObject);
			}
			trace(" testing seeking to AC");
			myKey = new bTreeStringKey("AC");
			myData = String(myBtree.seek(myKey));
			trace(myData+" should be ACData"(myData == "ACData"));
			// Step through the tree
			trace("step through the tree past that---------------------");
			while (myBtree.gotoNextKey() == 0) {
				trace(myBtree.currentKey+"  = "+myBtree.currentObject);
			}
			//////////////////////////////////////////////////////
			myBtree.seek(myKey);
			//trace(myData+" should be ACData"(myData == "ACData"));
			// Step through the tree
			trace("step through the tree in front of that---------------------");
			while (myBtree.gotoPrevKey() == 0) {
				trace(myBtree.currentKey+"  = "+myBtree.currentObject);
			}
			///////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////
			// Delete "Phil"
			trace("removing key AD and associated Data");
			myKey = new bTreeStringKey("AD");
			myBtree.removeItem(myKey);
			if (true) {
				trace("step through the tree---------------------");
				myBtree.rewind();
				while (myBtree.gotoNextKey() == 0) {
					trace(myBtree.currentKey+"  = "+myBtree.currentObject);
				}
				// Step through the tree backward
				trace("step through backwards 'AD' will be gone--------");
				myBtree.toEnd();
				while (myBtree.gotoPrevKey() == 0) {
					trace(myBtree.currentKey+" = "+myBtree.currentObject);
				}
			}
			break;
		}
	}
}
